<?php

/**
 * @Author: kendy
 * @Date:   2020-03-26 08:54:21
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-03-30 17:04:02
 */
namespace app\api\job;
use think\queue\Job;
use think\Log;
use think\Db;
use think\loader;
use app\common\model\OrderRefund as orf;
use app\api\model\OrderRefund as ore;
class Order{
	public function fire(Job $job,$data){
		//如有必要，可以根据业务去求和数据库中的最新数据，判断该任务是否仍有必要执行
		$isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data);

		if(!$isJobStillNeedToBeDone){
              $job->delete();
              return;
		}
		$isJobDone = $this->doHelloJob($data);
		if($isJobDone){
			//如果任务成功，记得删除任务
			$job->delete();
		}else{
			if($job->attempts() > 3){
				//通过这个方法可以检查这个任务已经重试几次了
				$job->delete();
			}
		}
	}
    /**
     * 有些消息在到达消费者时，可能已经不在需要执行了
     * [checkDatabaseToSeeIfJobNeedToBeDone description]
     * @Author   kendy
     * @DateTime 2020-03-26T09:03:20+0800
     * @param    [type]                   $data [发布任务时自定义的数据]
     * @return   [type]                         [任何执行的结果]
     */
	public function checkDatabaseToSeeIfJobNeedToBeDone($data){
		return true;
	}

   private function doHelloJob($data){

   	//根据消息中的数据进行实际的业务处理   
     //查看该订单的状态是否为待确认，
       $hotelOrder =   Db::name('hotel_order')
       ->where(['order_id'=>$data['order_id'],'order_status'=>6])
       ->find();  
       if(!empty($hotelOrder)){ 
       Db::startTrans();
       try{ 
              $send = [
                '1'=>$hotelOrder['room_name'],
                '2'=> $hotelOrder['order_sn']
            ];  
           $datas['order_status'] = 5;
           $datas['cancel_id'] = 3;  
            $orf = new orf();
             /*$obj['order_sn'] = $hotelOrder['order_sn'];
              $obj['object_id'] = $hotelOrder['hotel_id']; 
              $obj['object_type'] = 4; 
              $obj['user_id'] = $hotelOrder['uid'];
              $obj['refund_status'] = 1;
              $obj['refund_reason'] = "商户未确认自动退款";
              $obj['order_price'] = $hotelOrder['order_price'];   
              $orderRefund = new ore(); 
              $orderRefund->addOrderRefund($obj);    
              */
           //退款 
           //查看支付类型
          $hotelOrder['refund_reason'] = "商户未确认自动退款";
          $payType = $hotelOrder['pay_type'];//1支付宝 2微信   
           if($payType == 1){
               $rs = $orf->aliRefund($hotelOrder);
          }
          if($payType == 2){  
              $rs = $orf->weixinRefund($hotelOrder);
          } 
             
           if($rs == 'SUCCESS'){ 
              Db::name('hotel')
              ->where('hotel_id',$hotelOrder['hotel_id'])
              ->setDec('sum_sale',1);  
              Db::name('hotel_order')
              ->where('order_id',$hotelOrder['order_id'])
              ->update($datas); 
             
           }else{ 
              return false; 
           }          

           //给用户发送短信
            sendJsmsMessage($hotelOrder['tel'],178110,$send);
            Db::commit();
            return true;
          }catch(\Exception $e){ 
            Db::rollback(); 
            return true; 
          }
       } 

       return false; 
      }

}